package 队列;

class Node{
    private int val;
    private Node next;

    public Node(int val) {
        this.val = val;
    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}
public class 单链表实现队列 {
    private Node first;//链表的头
    private Node last;//链表的尾

    //入队
    public void offer(int val) {
        //相当于是 尾插法
        //需要判断是不是 第一次插入
        Node node = new Node(val);
        if (this.first==null) {
            this.first=node;
            this.last=node;
        }else {
            this.last.setNext(node);
            last=node;
        }

    }

    //出队
    public int poll() {
        //判断当前队列是否为空
        if (isEmpty()) {
            throw new UnsupportedOperationException("队列为空");
        }
        int res=this.first.getVal();
        this.first=this.first.getNext();
        return res;
    }

    //查看队头元素
    public int peek() {
        if (this.first==null) {
            throw new UnsupportedOperationException("队列为空");
        }
        return this.first.getVal();

    }
    //判断队列是否为空
    public boolean isEmpty() {
        if (this.first==null) {
            return true;
        }
        return false;
    }
}
